package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.ExceptDAO;
import cn.edu.scau.cmi.hombio.dao.ExceptionTypeDAO;
import cn.edu.scau.cmi.hombio.dao.ExceptionTypeEmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.InspectionExceptionDAO;

import cn.edu.scau.cmi.hombio.domain.Except;
import cn.edu.scau.cmi.hombio.domain.ExceptionType;
import cn.edu.scau.cmi.hombio.domain.ExceptionTypeEmployee;
import cn.edu.scau.cmi.hombio.domain.InspectionException;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for ExceptionType entities
 * 
 */

@Service("ExceptionTypeService")

@Transactional
public class ExceptionTypeServiceImpl implements ExceptionTypeService {

	/**
	 * DAO injected by Spring that manages Except entities
	 * 
	 */
	@Autowired
	private ExceptDAO exceptDAO;

	/**
	 * DAO injected by Spring that manages ExceptionType entities
	 * 
	 */
	@Autowired
	private ExceptionTypeDAO exceptionTypeDAO;

	/**
	 * DAO injected by Spring that manages ExceptionTypeEmployee entities
	 * 
	 */
	@Autowired
	private ExceptionTypeEmployeeDAO exceptionTypeEmployeeDAO;

	/**
	 * DAO injected by Spring that manages InspectionException entities
	 * 
	 */
	@Autowired
	private InspectionExceptionDAO inspectionExceptionDAO;

	/**
	 * Instantiates a new ExceptionTypeServiceImpl.
	 *
	 */
	public ExceptionTypeServiceImpl() {
	}

	/**
	 * Save an existing ExceptionType entity
	 * 
	 */
	@Transactional
	public void saveExceptionType(ExceptionType exceptiontype) {
		ExceptionType existingExceptionType = exceptionTypeDAO.findExceptionTypeByPrimaryKey(exceptiontype.getId());

		if (existingExceptionType != null) {
			if (existingExceptionType != exceptiontype) {
				existingExceptionType.setId(exceptiontype.getId());
				existingExceptionType.setName(exceptiontype.getName());
				existingExceptionType.setMemo(exceptiontype.getMemo());
			}
			exceptiontype = exceptionTypeDAO.store(existingExceptionType);
		} else {
			exceptiontype = exceptionTypeDAO.store(exceptiontype);
		}
		exceptionTypeDAO.flush();
	}

	/**
	 * Delete an existing Except entity
	 * 
	 */
	@Transactional
	public ExceptionType deleteExceptionTypeExcepts(Integer exceptiontype_id, Integer related_excepts_id) {
		Except related_excepts = exceptDAO.findExceptByPrimaryKey(related_excepts_id, -1, -1);

		ExceptionType exceptiontype = exceptionTypeDAO.findExceptionTypeByPrimaryKey(exceptiontype_id, -1, -1);

		related_excepts.setExceptionType(null);
		exceptiontype.getExcepts().remove(related_excepts);

		exceptDAO.remove(related_excepts);
		exceptDAO.flush();

		return exceptiontype;
	}

	/**
	 * Save an existing InspectionException entity
	 * 
	 */
	@Transactional
	public ExceptionType saveExceptionTypeInspectionExceptions(Integer id, InspectionException related_inspectionexceptions) {
		ExceptionType exceptiontype = exceptionTypeDAO.findExceptionTypeByPrimaryKey(id, -1, -1);
		InspectionException existinginspectionExceptions = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(related_inspectionexceptions.getId());

		// copy into the existing record to preserve existing relationships
		if (existinginspectionExceptions != null) {
			existinginspectionExceptions.setId(related_inspectionexceptions.getId());
			existinginspectionExceptions.setMemo(related_inspectionexceptions.getMemo());
			related_inspectionexceptions = existinginspectionExceptions;
		}

		related_inspectionexceptions.setExceptionType(exceptiontype);
		exceptiontype.getInspectionExceptions().add(related_inspectionexceptions);
		related_inspectionexceptions = inspectionExceptionDAO.store(related_inspectionexceptions);
		inspectionExceptionDAO.flush();

		exceptiontype = exceptionTypeDAO.store(exceptiontype);
		exceptionTypeDAO.flush();

		return exceptiontype;
	}

	/**
	 * Save an existing Except entity
	 * 
	 */
	@Transactional
	public ExceptionType saveExceptionTypeExcepts(Integer id, Except related_excepts) {
		ExceptionType exceptiontype = exceptionTypeDAO.findExceptionTypeByPrimaryKey(id, -1, -1);
		Except existingexcepts = exceptDAO.findExceptByPrimaryKey(related_excepts.getId());

		// copy into the existing record to preserve existing relationships
		if (existingexcepts != null) {
			existingexcepts.setId(related_excepts.getId());
			existingexcepts.setBarcode(related_excepts.getBarcode());
			existingexcepts.setReportTime(related_excepts.getReportTime());
			existingexcepts.setMemo(related_excepts.getMemo());
			related_excepts = existingexcepts;
		}

		related_excepts.setExceptionType(exceptiontype);
		exceptiontype.getExcepts().add(related_excepts);
		related_excepts = exceptDAO.store(related_excepts);
		exceptDAO.flush();

		exceptiontype = exceptionTypeDAO.store(exceptiontype);
		exceptionTypeDAO.flush();

		return exceptiontype;
	}

	/**
	 * Delete an existing InspectionException entity
	 * 
	 */
	@Transactional
	public ExceptionType deleteExceptionTypeInspectionExceptions(Integer exceptiontype_id, Integer related_inspectionexceptions_id) {
		InspectionException related_inspectionexceptions = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(related_inspectionexceptions_id, -1, -1);

		ExceptionType exceptiontype = exceptionTypeDAO.findExceptionTypeByPrimaryKey(exceptiontype_id, -1, -1);

		related_inspectionexceptions.setExceptionType(null);
		exceptiontype.getInspectionExceptions().remove(related_inspectionexceptions);

		inspectionExceptionDAO.remove(related_inspectionexceptions);
		inspectionExceptionDAO.flush();

		return exceptiontype;
	}

	/**
	 * Return all ExceptionType entity
	 * 
	 */
	@Transactional
	public List<ExceptionType> findAllExceptionTypes(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<ExceptionType>(exceptionTypeDAO.findAllExceptionTypes(startResult, maxRows));
	}

	/**
	 * Load an existing ExceptionType entity
	 * 
	 */
	@Transactional
	public Set<ExceptionType> loadExceptionTypes() {
		return exceptionTypeDAO.findAllExceptionTypes();
	}

	/**
	 * Return a count of all ExceptionType entity
	 * 
	 */
	@Transactional
	public Integer countExceptionTypes() {
		return ((Long) exceptionTypeDAO.createQuerySingleResult("select count(o) from ExceptionType o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing ExceptionType entity
	 * 
	 */
	@Transactional
	public void deleteExceptionType(ExceptionType exceptiontype) {
		exceptionTypeDAO.remove(exceptiontype);
		exceptionTypeDAO.flush();
	}

	/**
	 * Delete an existing ExceptionTypeEmployee entity
	 * 
	 */
	@Transactional
	public ExceptionType deleteExceptionTypeExceptionTypeEmployees(Integer exceptiontype_id, Integer related_exceptiontypeemployees_id) {
		ExceptionTypeEmployee related_exceptiontypeemployees = exceptionTypeEmployeeDAO.findExceptionTypeEmployeeByPrimaryKey(related_exceptiontypeemployees_id, -1, -1);

		ExceptionType exceptiontype = exceptionTypeDAO.findExceptionTypeByPrimaryKey(exceptiontype_id, -1, -1);

		related_exceptiontypeemployees.setExceptionType(null);
		exceptiontype.getExceptionTypeEmployees().remove(related_exceptiontypeemployees);

		exceptionTypeEmployeeDAO.remove(related_exceptiontypeemployees);
		exceptionTypeEmployeeDAO.flush();

		return exceptiontype;
	}

	/**
	 * Save an existing ExceptionTypeEmployee entity
	 * 
	 */
	@Transactional
	public ExceptionType saveExceptionTypeExceptionTypeEmployees(Integer id, ExceptionTypeEmployee related_exceptiontypeemployees) {
		ExceptionType exceptiontype = exceptionTypeDAO.findExceptionTypeByPrimaryKey(id, -1, -1);
		ExceptionTypeEmployee existingexceptionTypeEmployees = exceptionTypeEmployeeDAO.findExceptionTypeEmployeeByPrimaryKey(related_exceptiontypeemployees.getId());

		// copy into the existing record to preserve existing relationships
		if (existingexceptionTypeEmployees != null) {
			existingexceptionTypeEmployees.setId(related_exceptiontypeemployees.getId());
			existingexceptionTypeEmployees.setIsExceptionManager(related_exceptiontypeemployees.getIsExceptionManager());
			related_exceptiontypeemployees = existingexceptionTypeEmployees;
		}

		related_exceptiontypeemployees.setExceptionType(exceptiontype);
		exceptiontype.getExceptionTypeEmployees().add(related_exceptiontypeemployees);
		related_exceptiontypeemployees = exceptionTypeEmployeeDAO.store(related_exceptiontypeemployees);
		exceptionTypeEmployeeDAO.flush();

		exceptiontype = exceptionTypeDAO.store(exceptiontype);
		exceptionTypeDAO.flush();

		return exceptiontype;
	}

	/**
	 */
	@Transactional
	public ExceptionType findExceptionTypeByPrimaryKey(Integer id) {
		return exceptionTypeDAO.findExceptionTypeByPrimaryKey(id);
	}
}
